package com.baidu.shoppingmall.role.controller;

import com.baidu.shoppingmall.role.domain.*;
import com.baidu.shoppingmall.role.service.PermissionService;
import com.baidu.shoppingmall.role.service.RoleService;
import com.baidu.shoppingmall.role.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleConller {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @GetMapping("/permissionall")
    public String getPermission(String name,String password){
        StringBuilder str =new StringBuilder();
        List<RoleVO> rolePOList=new ArrayList<>();
        List<PermissionVO> permissionVOList=new ArrayList<>();
        UserVO userVO = new UserVO();
        userVO.setName(name);
        userVO.setPassword(password);
        //判断登录用户的信息是否正确
        Boolean loginStatus=userService.login(name,password);
        if (loginStatus){
            String id=userService.queryId(name,password);
            //根据id查询用户权限
            List<RoleDTO> roleDTOList=roleService.queryRole(id);
            roleDTOList.forEach(dto->rolePOList.add(dto.clone(RoleVO.class)));

            //根据用户id查询用户权限下的角色
            List<PermissionDTO> permissionDTOList=permissionService.queryUser(id);
            permissionDTOList.forEach(dto->permissionVOList.add(dto.clone(PermissionVO.class)));
        }else {
            return "用户信息错误";
        }
        return str.append("用户权限"+rolePOList+"\n").append("用户权限下的角色"+permissionVOList).toString();
    }
}
